package com.jc.service.api;

import java.lang.reflect.InvocationTargetException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.jc.framework.web.ResponseBaseModel;
import com.jc.framework.web.ResponseResultModel;

/****
 * 
* @version:
* @Description: 本系统所有对外服务，均在这个入口进行实现,通过服务名对应的包路径反射找到服务对应的处理类  流程： 接收参数， 验签， 参数校验，服务处理，返回
* @author: zyf
* 2018年12月3日
*/

@Service
public class ApiImpl extends ApiAdapter implements Api {
	private final Logger LOGGER = LoggerFactory.getLogger(ApiImpl.class);
	
	@Value("${oms.port.authorization.config}")
	private String config; 
	
	private final String base_deal_package = "com.jc.service.";
	//api.product.add
	@SuppressWarnings("unchecked")
	public ResponseEntity<?> deal(ParamModel model){
		try {
			init(config);
			checkSign(model);
			@SuppressWarnings("rawtypes")
			Class<BaseDual> dealObject = (Class<BaseDual>)Class.forName(base_deal_package + model.getMethod());
			return dealObject.newInstance().dual(model.getData());
		}catch (Exception e) {
			if (e instanceof InvocationTargetException) {
                Throwable targetEx =((InvocationTargetException)e).getTargetException();
                if (targetEx != null) {
                	LOGGER.error("执行业务方法错误: {},{}",e.getMessage(),e);
                }
            } else {
            	LOGGER.error("执行业务方法错误: {},{}",e.getMessage(),e);
            }
			
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	
	
	
	
	
	
}
